﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace JCSelector
{
    // Copyright(c) 2010 Raymond L. Brady
    // Please read the copyright that has been included with this project before modifying or distributing this program.
    //

    public delegate void JCSelectedItemHandler(object sender, EventArgs e, string JCItem);

    public partial class ItemSelectionForm : Form
    {
        private int mTotalPages;
        private int mCurrentPage;
        private double mPageSize = 10; // max 10 items per page allowed.
        private List<string> mItems;
        private Font mButtonFont = new Font("Arial",8); // default button font

        public event JCSelectedItemHandler Selected;

        public ItemSelectionForm()
        {
            InitializeComponent();
        }

#region "Event Code"
        // called when ever the item is selected
        protected virtual void OnSelected(EventArgs e, string JCItem)
        {
            // if not null then a client has hooked up a delegate to the event so call it.
            if(Selected != null) Selected(this, e, JCItem);
            this.Close();
        }
#endregion

#region "Public Properties"
        [Category("JC Touch Selector")]
        [Description("List of string items to be shown.")]
        public List<string> JCItems
        {
            // when the list object is set we setup paging for the object.
            // start on the first page.
            get { return this.mItems; }
            set {
                    this.mItems = value;
                    if (this.mItems.Count > 0)
                    { 
                        if(this.mItems.Count > 10) // If more than 10 items then calculate number of pages
                        {
                            this.mTotalPages = (int)Math.Ceiling((double)this.mItems.Count / mPageSize);
                        }    else {
                            this.mTotalPages = 1;
                        }
                        this.mCurrentPage = 1; // set the current page to the first page
                        this.UpdatePagingInfo();
                        // build the first page
                        this.ShowItemPage(mCurrentPage);
                    }
            }
        }

        [Category("JC Touch Selector")]
        [Description("Gets or sets the font for the item buttons.")]
        public Font Button_Font
        {
            get { return this.mButtonFont; }
            set { this.mButtonFont = value; this.ChangeButtonFont(value); }
        }
        #endregion

#region "Private Methods"

        private void ShowItemPage(int p)
        {
            int i;

            if (p > 1)
            {
                i = (p * 10) - 10; // p is the page. Multiply by ten to get the first item for the page.
            }
            else
            {
                i = 0;
            }

            this.TryButton(ref this.button1, i++);
            this.TryButton(ref this.button2, i++);
            this.TryButton(ref this.button3, i++);
            this.TryButton(ref this.button4, i++);
            this.TryButton(ref this.button5, i++);
            this.TryButton(ref this.button6, i++);
            this.TryButton(ref this.button7, i++);
            this.TryButton(ref this.button8, i++);
            this.TryButton(ref this.button9, i++);
            this.TryButton(ref this.button10, i++);
        }
        private void TryButton(ref Button pButton, int i)
        {
            // i is larger than the number of items in the list
            // then we disable the button. We have reached the
            // end of the list.
            if (i > (this.mItems.Count -1))
            {
                pButton.Text = string.Empty;
                pButton.Enabled = false;
            }
            else
            {
                pButton.Text = this.mItems[i];
                pButton.Enabled = true;
            }
        }
        private void UpdatePagingInfo()
        {
            this.JCPagingLabel.Text = string.Format("Showing Page {0} of {1}", this.mCurrentPage ,this.mTotalPages);
        }
        private void ChangeButtonFont(Font font)
        {
            this.button1.Font = font;
            this.button2.Font = font;
            this.button3.Font = font;
            this.button4.Font = font;
            this.button5.Font = font;
            this.button6.Font = font;
            this.button7.Font = font;
            this.button8.Font = font;
            this.button9.Font = font;
            this.button10.Font = font;

        }
#endregion

#region "Click Code"

        private void ItemButton_Click(object sender, EventArgs e)
        {
            Button button = (Button)sender;
            OnSelected(e, button.Text);
            this.mCurrentPage = 1; // set the current page back to 1
            this.UpdatePagingInfo();
            this.ShowItemPage(this.mCurrentPage); // back to the first page.
        }
        private void JCBackButton_Click(object sender, EventArgs e)
        {
            if (this.mCurrentPage > 1)
            {
                this.mCurrentPage--;
                this.ShowItemPage(this.mCurrentPage);
                this.UpdatePagingInfo();
            }
        }
        private void JCForwardButton_Click(object sender, EventArgs e)
        {
            if (this.mCurrentPage < this.mTotalPages)
            {
                this.mCurrentPage++;
                this.ShowItemPage(this.mCurrentPage);
                this.UpdatePagingInfo();
            }
        }

#endregion

    }
}
